const App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnValue:"获取验证码",
    code:"",
    phone:"",
    second: 60,
    btnDisabled:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  bindphone:function(){
    var _this = this;
    console.log("ss",this.data);
    var phone = this.data.phone;
    var code = this.data.code;

    if(phone==""){
      App.showError("请输入手机号");
      return;
    }
    if(code==""){
      App.showError("请输入验证码");
      return;
    }
    var token = wx.getStorageSync('token');
    App._post_form('wxapi/bindphone', {
      token:token,phone:phone,code:code
    }, function(result) {
        console.log("bindphone",result);
        if(result.status=="200"){
       
          wx.showToast({
            title: '绑定成功',
            icon: 'success',
            duration: 2000
          })
         setTimeout(() => {
          wx.navigateBack();
         }, 1000);
        }else{
          App.showError(result.message);
        }
    });
  },
  getcode:function(){
    console.log('获取验证码');
    var phone = this.data.phone;
    if(phone==""){
      App.showError("请输入手机号");
      return;
    }
    var that = this;
    App._post_form('getcode', {
      phone:phone
    }, function(result) {
        console.log("bindphone",result);
        if(result.status=="200"){
          that.timer();
        }else{

        }
    });
  },
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          var second = this.data.second - 1;
          this.setData({
            second: second,
            btnValue: second+'秒',
            btnDisabled: true
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              btnValue: '获取验证码',
              btnDisabled: false
            })
            resolve(setTimer)
          }
        }
        , 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },
  setphone:function(e){
    console.log("e",e);
    this.setData({phone: e.detail})

   
  },
  setcode:function(e){
    this.setData({code: e.detail})
  }
})